/*
    Copyright Nicolas Ronvel 2007
	gulix33xp@yahoo.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections.Generic;
using System.Drawing;

using SdlDotNet.Graphics;
using SdlDotNet.Input;
using SdlDotNet.Core;

using BlindShark.GameElements;
using BlindShark.Widgets;

namespace BlindShark.GameStates
{
	/// <summary>
	/// Description of GS_SelectPlayers.
	/// </summary>
	public class GS_SelectPlayers : GameState
	{
		// Font file
		string _sFont;
		
		// Surfaces
		Surface _sfFrame;
		
		// Elements Location
		Rectangle _rctFrame;
		
		// Gallery of profiles
		List<ProfileFile> _lsProfiles;
		ImageGallery _glProfiles;
		List<PlayerCard> _lsPlayerCards;
		
		// Labels
		Label _lblTitle;
		Label _lblPlayerName;
		
		// Buttons
		Button _btnAddPlayer;
		Button[] _btnRemovePlayer;
		Button _btnPlay;
		Button _btnBack;
		int _iActiveRemove;
		
		public GS_SelectPlayers()
		{	
			_sFont = "fonts/Fertigo_17b.otf";
			Label.DefaultFont = _sFont;
			
			// Initializing the labels
			// Title of the screen
			_lblTitle = new Label(LanguageManager.Get("SLP_Title"), 36, Color.OrangeRed, TextAlignment.TopMiddle);
			_lblTitle.Underline = true;
			_lblTitle.AutoHeight = true;
			// The name of the selected player
			_lblPlayerName = new Label("", 22, Color.Orange, TextAlignment.CenterMiddle);
			_lblPlayerName.AutoHeight = true;
			
			// Initializing the Gallery
			_glProfiles = new ImageGallery();
			_glProfiles.BackColor = Color.Black;
			_glProfiles.BorderColor = Color.Gold;
			_glProfiles.BorderSize = 2;
			_glProfiles.Size = new Size(404, 204);
			_glProfiles.CenterImageSize = new Size(180, 180);
			_glProfiles.SideImageSize = new Size(90, 90);
			_glProfiles.SelectedIndexChanged += new ImageGallery.SelectedIndexChangedEvent(DisplayPlayer);
			
			// Initializing the buttons
			// "Add Player" Button
			_btnAddPlayer = new Button("gfx/buttons1.jpg", true);
			_btnAddPlayer.Font = _sFont;
			_btnAddPlayer.Text = LanguageManager.Get("SLP_AddPlayer");
			_btnAddPlayer.TextColor = Color.Orange;
			_btnAddPlayer.TextSize = 20;
			_btnAddPlayer.Click += new Button.ClickEvent(AddPlayer);
			// "Remove Player" Buttons
			_btnRemovePlayer = new Button[4];
			for(int i=0; i<4; i++)
			{
				_btnRemovePlayer[i] = new Button("gfx/buttons1.jpg", true);
				_btnRemovePlayer[i].Font = _sFont;
				_btnRemovePlayer[i].Text = LanguageManager.Get("SLP_RemovePlayer");
				_btnRemovePlayer[i].TextColor = Color.Orange;
				_btnRemovePlayer[i].TextSize = 20;
				_btnRemovePlayer[i].Click += new Button.ClickEvent(RemovePlayer);
				_btnRemovePlayer[i].Enabled = false;
			}
			_iActiveRemove = -1;
			// "Play" Button
			_btnPlay = new Button("gfx/buttons1.jpg", true);
			_btnPlay.Font = _sFont;
			_btnPlay.Text = LanguageManager.Get("SLP_Play");
			_btnPlay.TextColor = Color.ForestGreen;
			_btnPlay.TextSize = 20;
			_btnPlay.Click += new Button.ClickEvent(Play);
			// "Back" Button
			_btnBack = new Button("gfx/buttons1.jpg", true);
			_btnBack.Font = _sFont;
			_btnBack.Text = LanguageManager.Get("SLP_Back");
			_btnBack.TextColor = Color.Gainsboro;
			_btnBack.TextSize = 20;
			_btnBack.Click += new Button.ClickEvent(Back);
			
			// Initializing the Players' list
			LoadProfiles();
			
			_lsPlayerCards = new List<PlayerCard>();
		}
		
		#region GameState interface members
		public void Init()
		{
			// Displaying the background one for once
			ScreenManager.MainScreen.Blit(SurfaceManager.GetSurface("gfx/background.png"));
			
			// Initializing the main frame
			_sfFrame = new Surface(SurfaceManager.GetSurface("gfx/background_infoLongue.png").Size);
			_sfFrame.Blit(SurfaceManager.GetSurface("gfx/background_infoLongue.png"));
			_rctFrame = new Rectangle(new Point(48,48),_sfFrame.Size);
			
			// Initializing the title location
			_lblTitle.Location = new Point(0, 15);
			_lblTitle.Size = _rctFrame.Size;
			_lblTitle.Init();
			_sfFrame.Blit(_lblTitle.Surface, _lblTitle.Location);
			
			// Initializing the Gallery
			_glProfiles.Init();
			_glProfiles.X = _rctFrame.X + 20;
			_glProfiles.Y = _rctFrame.Y + _lblTitle.Bottom + 20;
			
			// Initializing the label for the Player Name
			_lblPlayerName.Location = new Point(_glProfiles.Right + 10, _glProfiles.Y + 10);
			_lblPlayerName.Size = new Size(_rctFrame.Right - _glProfiles.Right - 20, 32);			
			DisplayPlayer();
			
			// Initializing the AddPlayer Button
			_btnAddPlayer.Init();
			_btnAddPlayer.X = _glProfiles.Right + (_rctFrame.Right - _glProfiles.Right - _btnAddPlayer.Width) / 2;
			_btnAddPlayer.Y = _lblPlayerName.Bottom + 10;
			_btnPlay.Init();
			_btnPlay.Location = new Point(_btnAddPlayer.X, _btnAddPlayer.Bottom + 10);
			_btnBack.Init();
			_btnBack.Location = new Point(_btnPlay.X, _btnPlay.Bottom + 10);
			for(int i=0; i<4; i++)
				_btnRemovePlayer[i].Init();
			
		}
		
		public void Update()
		{
			
		}
		
		public void HandleEvents()
		{
			while(Events.Poll())
			{
				// Handling gallery events
				_glProfiles.HandleEvents();
				
				// Handling Buttons Events
				_btnAddPlayer.HandleEvents();
				_btnPlay.HandleEvents();
				_btnBack.HandleEvents();
				for(int i=0; i<4; i++)
				{
					_iActiveRemove = i;
					_btnRemovePlayer[i].HandleEvents();
				}
				_iActiveRemove = -1;
			}
		}
		
		public void Draw()
		{
			// Displaying the frame
			ScreenManager.MainScreen.Blit(_sfFrame, _rctFrame.Location);
			
			// Displaying the gallery
			ScreenManager.MainScreen.Blit(_glProfiles.Surface, _glProfiles.Location);
			
			// Displaying the Player Name
			ScreenManager.MainScreen.Blit(_lblPlayerName.Surface, _lblPlayerName.Location);
			
			// Displaying the buttons
			ScreenManager.MainScreen.Blit(_btnAddPlayer.Surface, _btnAddPlayer.Location);
			ScreenManager.MainScreen.Blit(_btnBack.Surface, _btnBack.Location);
			if (_lsPlayerCards.Count>=2)
				ScreenManager.MainScreen.Blit(_btnPlay.Surface, _btnPlay.Location);
			
			// Displaying the selected players
			for(int i=0; i<_lsPlayerCards.Count; i++)
			{
				ScreenManager.MainScreen.Blit(_lsPlayerCards[i].Surface, _lsPlayerCards[i].Location);
				ScreenManager.MainScreen.Blit(_btnRemovePlayer[i].Surface, _btnRemovePlayer[i].Location);
			}
		}
		
		public void CleanUp()
		{
			// Cleaning the surfaces
			_sfFrame = null;
			
			// Cleaning the labels
			_lblTitle.CleanUp();
			
			// Cleaning the buttons
			_btnAddPlayer.CleanUp();
			_btnPlay.CleanUp();
			_btnBack.CleanUp();
			for(int i=0; i<4; i++)
				_btnRemovePlayer[i].CleanUp();
		}
		
		#endregion
	
		#region Players Display functions
		private void DisplayPlayer()
		{
			_lblPlayerName.Text = _lsProfiles[_glProfiles.SelectedIndex].profile.Name;
			_lblPlayerName.Init();
		}
		
		private void LoadProfiles()
		{
			// Clearing the list
			_lsProfiles = new List<ProfileFile>();
			
			// Checking the existence of the directory
			System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo("profiles");
			if (! directory.Exists)
				throw new Exception("No Profiles Directory");
			
			// Scanning the directory to create the profiles
			foreach(System.IO.FileInfo file in directory.GetFiles("*.xml"))
			{
				ProfileFile profileFile;
				profileFile.profile = new Profile();
				profileFile.profile.ReadFromXml(file.FullName);
				profileFile.file = file.Name;
				_lsProfiles.Add(profileFile);
			}
			
			// Sorting the list
			_lsProfiles.Sort();
			for(int i=0; i<_lsProfiles.Count; i++)
			{
				_glProfiles.Add("avatars/"+_lsProfiles[i].profile.PictureFile);
			}
		}
		
		void SetCardsLocation()
		{
			for (int i=0; i<_lsPlayerCards.Count; i++)
			{
				_lsPlayerCards[i].Location = new Point(400 - 75 - 95*(_lsPlayerCards.Count-1) + i*190,
				                                       ScreenManager.Height-_lsPlayerCards[i].Height);
				_btnRemovePlayer[i].X = _lsPlayerCards[i].X + (_lsPlayerCards[i].Width - _btnRemovePlayer[i].Width) / 2;
				_btnRemovePlayer[i].Y = _lsPlayerCards[i].Y - 5 -_btnRemovePlayer[i].Height;
			}
			
			_btnAddPlayer.Enabled = _lsPlayerCards.Count<4;
		}
		#endregion
		
		#region Buttons Events
		public void AddPlayer()
		{
			Init();
			PlayerCard pc = new PlayerCard(new Player(_lsProfiles[_glProfiles.SelectedIndex].profile));
			pc.ShowScore = false;
			pc.Init();
			_lsPlayerCards.Add(pc);
			_btnRemovePlayer[_lsPlayerCards.Count-1].Enabled = true;
			
			SetCardsLocation();
		}
		
		public void RemovePlayer()
		{
			Init();
			if ((_iActiveRemove<0) || (_iActiveRemove>=_lsPlayerCards.Count))
				return;
			
			_lsPlayerCards.RemoveAt(_iActiveRemove);
			_btnRemovePlayer[_lsPlayerCards.Count].Enabled = false;
			
			SetCardsLocation();
			
		}
		
		public void Back()
		{
			GameStateManager.ChangeState(new GS_MenuPlay());
		}
		
		public void Play()
		{
			foreach(PlayerCard pc in _lsPlayerCards)
					PlayerManager.AddPlayer(pc.Player);
			GameStateManager.ChangeState(new GS_SelectTracklist());
		}
		#endregion
	}
}
